package org.desktop.web.client.project.window;

import org.desktop.web.client.custom.widget.AbstractWindow;
import org.desktop.web.client.images.Resources;
import org.desktop.web.client.project.form.ProjectFormWindow;
import org.desktop.web.client.project.form.SubProjectFormWindow;
import org.desktop.web.client.project.grid.ProjectGrid;
import org.desktop.web.client.project.grid.SubProjectsGrid;
import org.desktop.web.client.project.service.ProjectService;
import org.desktop.web.client.project.service.ProjectServiceAsync;
import org.desktop.web.client.project.window.handler.RemoveSubprojectHandler;
import org.desktop.web.shared.domain.entities.Project;
import org.desktop.web.shared.domain.entities.SubProject;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.MinimizeEvent;
import com.sencha.gxt.widget.core.client.event.MinimizeEvent.MinimizeHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

public class ProjectWindow extends AbstractWindow {

	private FramedPanel panel;

	private SubProjectsGrid spGrid;

	private ProjectGrid pGrid;

	public ProjectWindow() {
	}

	public void init() {

		this.setWidth(790);
		this.setHeight(480);
		this.panel = new FramedPanel();
		this.panel.setHeaderVisible(false);
		this.spGrid = new SubProjectsGrid(20, null, true);
		this.pGrid = new ProjectGrid(20, spGrid, true);

		TextButton addProject = new TextButton();
		addProject.setIcon(Resources.IMAGES.add());
		addProject.setText("Add project");

		ToolBar projectToolBar = createProjectToolbar(addProject);
		TextButton removeProject = new TextButton("Remove");
		removeProject.setIcon(Resources.IMAGES.delete());
		projectToolBar.add(removeProject);

		TextButton addSubProject = new TextButton();
		addSubProject.setIcon(Resources.IMAGES.add());
		addSubProject.setText("Add sub project");

		TextButton removeSubProject = new TextButton("Remove");
		removeSubProject.setIcon(Resources.IMAGES.delete());

		ToolBar subProjectToolbar = createProjectToolbar(addSubProject);
		subProjectToolbar.add(removeSubProject);

		VerticalLayoutContainer mainPanel = new VerticalLayoutContainer();

		VerticalLayoutContainer projectContainer = new VerticalLayoutContainer();
		VerticalLayoutContainer subProjectContainer = new VerticalLayoutContainer();

		projectToolBar.setLayoutData(new VerticalLayoutData(1, -1));
		projectContainer.add(projectToolBar);
		subProjectContainer.add(subProjectToolbar);
		// BorderLayoutContainer con = new BorderLayoutContainer();

		ContentPanel projectPanel = new ContentPanel();
		projectPanel.setHeaderVisible(false);
		projectPanel.add(pGrid.asWidget());

		ContentPanel subProjectPanel = new ContentPanel();
		subProjectPanel.setHeaderVisible(false);
		subProjectPanel.add(spGrid.asWidget());

		// gli eventi sui button della toolbar vanno aggiunti inquesto punto,
		// dopo la chiamata al metodo asWidget

		final Grid<SubProject> child = spGrid.getGrid();
		final Grid<Project> parent = pGrid.getGrid();

		addSubProject.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (parent.getSelectionModel().getSelectedItem() == null) {
					Window.alert("Nessun progetto selezionato");
					return;
				}
				SubProjectFormWindow form = new SubProjectFormWindow(parent
						.getSelectionModel().getSelectedItem(), spGrid);
				form.init();
				form.show();

			}
		});

		removeSubProject.addSelectHandler(new RemoveSubprojectHandler(spGrid,
				pGrid));

		addProject.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				ProjectFormWindow form = new ProjectFormWindow(pGrid);
				form.init();
				form.show();

			}
		});

		removeProject.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (parent.getSelectionModel().getSelectedItem() == null) {
					AlertMessageBox b = new AlertMessageBox("Error",
							"Selezionare la riga da eliminare");
					b.setIcon(AlertMessageBox.ICONS.warning());
					b.setModal(true);
					b.show();
					return;
				}

				ProjectServiceAsync service = GWT.create(ProjectService.class);

				service.deleteProject(parent.getSelectionModel()
						.getSelectedItem(), new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							pGrid.load();
						}
					}
				});

			}
		});
		VerticalLayoutData vldProjectGrid = new VerticalLayoutData(1.0, 0.5,
				new Margins(5, 0, 5, 0));
		VerticalLayoutData vldSubProjectGrid = new VerticalLayoutData(1.0, 0.5);

		projectContainer.add(projectPanel, new VerticalLayoutData(1, 1));
		projectContainer.add(pGrid.getPagingToolBar(), new VerticalLayoutData(
				1, -1));
		subProjectContainer.add(subProjectPanel, new VerticalLayoutData(1, 1));
		subProjectContainer.add(spGrid.getPagingToolBar(),
				new VerticalLayoutData(1, -1));

		mainPanel.add(projectContainer, vldProjectGrid);
		mainPanel.add(subProjectContainer, vldSubProjectGrid);

		this.panel.add(mainPanel);
		this.add(panel);
		// TODO Auto-generated constructor stub
	}

	public ToolBar createProjectToolbar(TextButton aButton) {
		ToolBar aToolBar = new ToolBar();

		aToolBar.add(aButton);
		return aToolBar;
	}

	@Override
	public String id() {
		// TODO Auto-generated method stub
		return "PROJECTS";
	}

}
